package com.wxx.service.tool.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.google.common.collect.Maps;
import com.wxx.common.Const;
import com.wxx.model.sdk.AliPayModel;
import com.wxx.model.sdk.AliPayTransfer;
import com.wxx.util.PropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class AlipayServiceImpl implements AlipayService {

    private final String serverUrl = PropertiesUtil.getProperty("ALIPAY_URL");
    private final String appId = PropertiesUtil.getProperty("ALIPAY_APPID");
    private final String privateKey = PropertiesUtil.getProperty("ALIPAY_PRIVATEKEY");
    private final String alipayPublicKey = PropertiesUtil.getProperty("ALIPAY_PUBLICKEY");

    Map<String,Object> map =null;

    public Map toPay(AliPayModel aliPayModel) {
        map = new HashMap<String,Object>();
        //实例化客户端
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, "json", "UTF-8", alipayPublicKey, "RSA2");
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(aliPayModel.getBody());
        model.setSubject(aliPayModel.getSubject());
        model.setOutTradeNo(aliPayModel.getOutTradeNo());
        model.setTimeoutExpress("30m");//默认30分钟失效
        model.setTotalAmount(aliPayModel.getTotalAmount());
        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setBizModel(model);
        request.setNotifyUrl(aliPayModel.getNotifyUrl());
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            System.out.println(response.getBody());//就是orderString 可以直接给客户端请求，无需再做处理。
            if (response.isSuccess()){
                map.put("code", Const.SUCCESS_CODE);
                map.put("body",response.getBody());
            }else{
                map.put("code", Const.ERROR_CODE);
                map.put("body",response.getBody());
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return map;
    }


    @Override
    public Map<String, Object> transToAccount(AliPayTransfer aliPayTransfer) {
        map = Maps.newHashMap();
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey,"json","GBK",alipayPublicKey,"RSA2");
        AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();
        request.setBizContent("{" +
                "\"out_biz_no\":\""+aliPayTransfer.getOutBizNo()+"\"," +
                "\"payee_type\":\"ALIPAY_LOGONID\"," +
                "\"payee_account\":\""+aliPayTransfer.getPayeeAccount()+"\"," +
                "\"amount\":\""+aliPayTransfer.getAmount()+"\"," +
                "\"payer_show_name\":\""+aliPayTransfer.getPayerShowName()+"\"," +
                "\"payee_real_name\":\""+aliPayTransfer.getPayeeRealName()+"\"," +
                "\"remark\":\""+aliPayTransfer.getRemark()+"\"" +
                "}");
        try {
            AlipayFundTransToaccountTransferResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                map.put("code", Const.SUCCESS_CODE);
                map.put("body",response.getBody());
            } else {
                map.put("code", Const.ERROR_CODE);
                map.put("body",response.getBody());
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return map;
    }
}
